package com.scaffold.authority.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import wangjg.commons.page.Page;

import com.scaffold.authority.entity.Resource;

@Repository
public class ResourceDAO extends BasicBizDAO<Resource, Long> {

	public List<Resource> pageQuery(Page page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}

	public List<Resource> pageQuery(Page page, String value, Integer type,String keyword) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		if (value != null) {
			criteria.add(Restrictions.eq("value", value));
		}
		if (type != null) {
			criteria.add(Restrictions.eq("type", type));
		}
		if (keyword != null && !keyword.isEmpty()) {
			LogicalExpression expr = Restrictions.or(
				Restrictions.like("name", keyword, MatchMode.ANYWHERE).ignoreCase(),
				Restrictions.like("value", keyword, MatchMode.ANYWHERE).ignoreCase()
			);
			criteria.add(expr);
		}
		criteria.addOrder(Order.asc("value"));
		return this.findByCriteria(criteria, page);
	}

}
